package com.example.KeHuDuan;

import androidx.annotation.NonNull;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import gson.Test.Video;

import static gson.client.InstructionList.DATA_OP.GET_LIST;

public class VideoPlayer extends BaseActivity implements View.OnClickListener, AbsListView.OnScrollListener{

    private static final String TAG = "VideoPlayer";

    private boolean IsPlay = false;

    private List<Video> mList = new ArrayList<>();

    ListView list_view;

    Button playOrPause;

    private static final int PAGE_SIZE = 20;

    private int mCurrentStartNo = -1;

    VideoAdapter myAdapter;

    private int currentVideoIndex = -1;

    ListView listView;
    TextView videoName;
    Button previous;
    Button next;
    Button back;
    
    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GET_LIST:
                    mList.addAll((List<Video>) msg.obj);
                    myAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected  void sendHandlerToService(){
        mServiceBinder.sendHandler(mHandler);
        getVideoListFromService(0 /* first request */);
    }

    @SuppressLint("CutPasteId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        back = (Button) findViewById(R.id.back);
        listView = (ListView) findViewById(R.id.list_view);
        videoName = (TextView) findViewById(R.id.video_name);
        previous = (Button) findViewById(R.id.previous);
        playOrPause = (Button) findViewById(R.id.play_or_pause);
        next = (Button) findViewById(R.id.next);
        list_view = findViewById(R.id.list_view);
        back.setOnClickListener(this);
        previous.setOnClickListener(this);
        playOrPause.setOnClickListener(this);
        next.setOnClickListener(this);
        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                mServiceBinder.sVideoPlay(position);
                changeUI(position);
            }
        });
        initList();
    }

    private void getVideoListFromService(int start_no) {
        if (mServiceBinder != null && start_no > mCurrentStartNo) {
            mCurrentStartNo = start_no;
            mServiceBinder.sGetVideoList(start_no, PAGE_SIZE);
        }
    }

    private void initList() {
        Log.d(TAG, "showList:is create ");
        myAdapter = new VideoAdapter(VideoPlayer.this, mList);
        list_view.setAdapter(myAdapter);
        list_view.setOnScrollListener(this);

    }

    private void changeUI(int index){
        videoName.setText(mList.get(index).getVideoName());
        playOrPause.setBackgroundResource(R.drawable.ic_pause);

    }

    public void onClick(View view){
        switch(view.getId()){
            case R.id.back:
                Intent backIntent=new Intent(VideoPlayer.this,MainActivity.class);
                startActivity(backIntent);
                mServiceBinder.stop();
                onBackPressed();
                break;
            case R.id.previous:
                if (currentVideoIndex == 0) {
                    currentVideoIndex = mList.size() - 1;
                } else {
                    currentVideoIndex--;
                }
                mServiceBinder.sVideoPrevious();
                IsPlay = true;
                changeUI(currentVideoIndex);
                break;
            case R.id.play_or_pause:
                if (IsPlay == false) {
                    mServiceBinder.sVideoContinue();
                    IsPlay = true;
                    playOrPause.setBackgroundResource(R.drawable.ic_pause);
                } else {
                    mServiceBinder.sVideoPause();
                    IsPlay = false;
                    playOrPause.setBackgroundResource(R.drawable.ic_play);
                }

                break;
            case R.id.next:
                if (currentVideoIndex == mList.size() - 1) {
                    currentVideoIndex = 0;
                } else {
                    currentVideoIndex++;
                }
                mServiceBinder.sVideoNext();
                IsPlay = true;
                changeUI(currentVideoIndex);
                break;
            default:
                break;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if (i == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
            if (absListView.getLastVisiblePosition() == mList.size() - 1) {
                getVideoListFromService(mList.size());
            }
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {

    }
}
